package com.ether.controller;

import com.alibaba.fastjson.JSONObject;
import com.ether.entity.ClinicalTrials;
import com.ether.entity.DeclarationProgress;
import com.ether.entity.DomesticProjectProgress;
import com.ether.entity.MedInvest;
import com.ether.mapper.ClinicalTrialsMapper;
import com.ether.mapper.DeclarationProgressMapper;
import com.ether.mapper.DomesticProjectProgressMapper;
import com.ether.mapper.MedInvestMapper;
import com.ether.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping(value="/cor")
public class MedInvestRestController {

    @Autowired
    private MedInvestMapper medInvestMapper;

    @Autowired
    private ClinicalTrialsMapper clinicalTrialsMapper;

    @Autowired
    private DeclarationProgressMapper declarationProgressMapper;

    @Autowired
    private DomesticProjectProgressMapper domesticProjectProgressMapper;

    @RequestMapping("/findByCorname")
    public JSONObject findByCorname(@RequestParam(value = "corname") String corname,
                                    @RequestParam(value = "comp") String comp,
                                    @RequestParam(value = "f12") String f12,
                                    @RequestParam(value = "f19") String f19) {
        JSONObject result = new JSONObject();

        List<DomesticProjectProgress> domesticProjectProgressList = domesticProjectProgressMapper.findByCorname(corname, comp, f12, f19);

        String f3 = "";
        for(DomesticProjectProgress domesticProjectProgress : domesticProjectProgressList) {
            String f24 = StringUtils.trim(domesticProjectProgress.getF24()).replaceAll("，" , ","); // 临床申请
            List<DeclarationProgress> declarationProgressList = declarationProgressMapper.findByCorname(corname, f24);
            domesticProjectProgress.setDeclarationProgressList(declarationProgressList);

            String f26 = StringUtils.trim(domesticProjectProgress.getF26()).replaceAll("，" , ","); // 临床试验
            List<ClinicalTrials> clinicalTrialsList = clinicalTrialsMapper.findByCorname(corname, f26);
            domesticProjectProgress.setClinicalTrialsList(clinicalTrialsList);

            String f29 = StringUtils.trim(domesticProjectProgress.getF29()).replaceAll("，" , ","); // 上市申请
            List<DeclarationProgress> drugApplicationList = declarationProgressMapper.findByCorname(corname, f29);
            domesticProjectProgress.setDrugApplicationList(drugApplicationList);

            f3 += "," + StringUtils.trim(domesticProjectProgress.getF3());
        }
        result.put("domesticProjectProgressList", domesticProjectProgressList);

        if(StringUtils.isEmpty(f3)) {
            result.put("medInvestList", null);
        }else {
            List<MedInvest> medInvestList = medInvestMapper.findByCorname(corname, f3);
            result.put("medInvestList", medInvestList);
        }

        return result;
    }

    @RequestMapping("/getReportResult")
    public JSONObject getReportResult() {
        JSONObject result = new JSONObject();

        int projectNum = domesticProjectProgressMapper.getProjectNum();

        int corNum = domesticProjectProgressMapper.getCorNum();

        int medCorNum = medInvestMapper.getMedCorNum();

        int investNum = medInvestMapper.getInvestNum();

        int caNum = domesticProjectProgressMapper.getCANum();

        int ctNum = domesticProjectProgressMapper.getCTNum();

        int ctaNum = clinicalTrialsMapper.getCTANum();

        result.put("projectNum", projectNum);
        result.put("corNum", corNum);
        result.put("medCorNum", medCorNum);
        result.put("investNum", investNum);
        result.put("caNum", caNum);
        result.put("ctNum", ctNum);
        result.put("ctaNum", ctaNum);

        return result;
    }
}
